package com.fengxing.ams.web.facade.mouble.enrollment;

import java.io.UnsupportedEncodingException;
import java.util.Set;

import org.springframework.stereotype.Repository;

import redis.clients.jedis.Jedis;

import com.fengxing.ams.web.common.SerializeUtil;
import com.fengxing.ams.web.facade.mouble.BaseRedis;

@Repository
public class MajorEnrollmentRedis extends BaseRedis implements
		IMajorEnrollmentRedis {

	@Override
	public boolean isExists(MajorEnrollmentVO me) {
		Jedis jedis = this.getJedis();
		
		try {
			Set<byte[]> set = jedis.zrangeByScore(ENROLLMENTS.getBytes("UTF-8"), me.getId(), me.getId());
			return set.size() == 1;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} finally {
			this.closeJedis(jedis);
		}
		return false;
	}

	@Override
	public void refreshCache(MajorEnrollmentVO me) {
		Jedis jedis = this.getJedis();
		
		try {
			jedis.zadd(ENROLLMENTS.getBytes("UTF-8"), me.getId(), SerializeUtil.serialize(me));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} finally {
			this.closeJedis(jedis);
		}
	}

	@Override
	public MajorEnrollmentVO getCache(Integer id) {
		Jedis jedis = this.getJedis();
		
		try {
			Set<byte[]> b = jedis.zrangeByScore(ENROLLMENTS.getBytes("UTF-8"), id, id);
			return (MajorEnrollmentVO) SerializeUtil.unserialize((byte[])b.toArray()[0]);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} finally {
			this.closeJedis(jedis);
		}
		return null;
	}
	
}
